<?php
declare(strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class SendMessageLog extends Model
{
    protected $schema = [
        'id'           => 'int',
        'user_id'      => 'string',
        'template_id'  => 'string',
        'status'       => 'string',
        'created_time' => 'timestamp',
    ];

    public function getById($id)
    {
        $setting = self::findOrEmpty($id);
        if (empty($setting)) {
            return [];
        }
        return $setting->toArray();
    }

    public function findByIds(array $ids)
    {
        $settings = self::select($ids);
        if ($settings->isEmpty()) {
            return [];
        }
        return $settings->toArray();
    }

    public function findByUserId($userId)
    {
        $setting = self::where('user_id', $userId)->findOrEmpty();
        if (empty($setting)) {
            return [];
        }
        return $setting->toArray();
    }

    public function findByTemolateId($temolateId)
    {
        $setting = self::where('temolateId', $temolateId)->findOrEmpty();
        if (empty($setting)) {
            return [];
        }
        return $setting->toArray();
    }

    public function getByFields($field, $value)
    {
        $setting = self::where($field, $value)->findOrEmpty();
        if (empty($setting)) {
            return [];
        }
        return $setting->toArray();
    }

    public function countSendMessageLog(array $conditions)
    {
        return $this->countData($conditions);
    }

    public function searchSendMessageLogs(array $conditions, array $orderBy = [], $start = 0, $limit = 20): array
    {
        $templates = $this->searchData($conditions, $orderBy, $start, $limit);

        if (empty($templates)) {
            return [];
        }
        return $templates->toArray();
    }

    public function declares(): array
    {
        return [
            'orderBy' => [
                'id',
                'created_time'
            ],
            'conditions' => [
                'id' => ['=', 'id'],
                'ids' => ['in', 'id'],
                'user_id' => ['=', 'user_id'],
                'template_id' => ['=', 'template_id'],
            ]
        ];
    }
}
